package com.itheima;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;

public class ApachePOIDemo {

    /**
     *  封装了几个核心对象
     *      XSSFWorkbook：工作簿
     *      XSSFSheet：工作表
     *      Row：行
     *      Cell：单元格
     * @param args
     * @throws IOException
     */
    public static void main1(String[] args) throws IOException {

        //获取工作簿对象【excel】
        XSSFWorkbook workbook = new XSSFWorkbook("C:\\Users\\123\\Desktop\\1.xlsx");

        //获取到表
        XSSFSheet sheet = workbook.getSheetAt(0);

        //遍历所有的行
        for (int i = 0; i <= sheet.getLastRowNum(); i++) {
            //获取到每一行的数据
            XSSFRow row = sheet.getRow(i);
            //System.out.println(row);

            //获取到每一行有多少个单元格
            //short cellNum = row.getLastCellNum();
            //System.out.println(cellNum);

            //获取到每一行有多少个单元格  遍历这些单元格
            for (int j = 0; j < row.getLastCellNum(); j++) {
                XSSFCell cell = row.getCell(j);
                //System.out.println(cell);

                //如果单元格是数值类型，就让他变成字符串
                if (cell.getCellType() == cell.CELL_TYPE_NUMERIC){
                    cell.setCellType(cell.CELL_TYPE_STRING);
                }

                //获取到单元格里面的值
                String cellValue = cell.getStringCellValue();
                System.out.println(cellValue);

            }

        }

    }


    public static void main2(String[] args) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook("C:\\Users\\123\\Desktop\\1.xlsx");

        //获取工作表
        XSSFSheet sheet = workbook.getSheetAt(1);

        //获取每一行
        for (Row row : sheet) {
            //获取每一个单元格对象
            for (Cell cell : row) {
                //设置单元格为字符串
                if (cell.getCellType() == cell.CELL_TYPE_NUMERIC){
                    cell.setCellType(cell.CELL_TYPE_STRING);
                }
                String cellValue = cell.getStringCellValue();
                System.out.println(cellValue);
            }
        }

    }

    /**
     * 写入数据到EXCEL
     * @param args
     */
    public static void main(String[] args) throws IOException {
        //创建工作簿对象
        XSSFWorkbook workbook = new XSSFWorkbook();

        //创建工作表对象
        XSSFSheet sheet = workbook.createSheet("test");

        //创建行
        XSSFRow row = sheet.createRow(1);
        //创建单元格
        XSSFCell cell1 = row.createCell(5);
        //给cell赋值
        cell1.setCellValue("随便");

        //创建单元格
        XSSFCell cell2 = row.createCell(6);
        //给cell赋值
        cell2.setCellValue("随便1");


        //创建行
        XSSFRow row2 = sheet.createRow(2);
        //创建单元格
        XSSFCell cell3 = row2.createCell(5);
        //给cell赋值
        cell3.setCellValue("都行");

        //创建单元格
        XSSFCell cell4 = row2.createCell(6);
        //给cell赋值
        cell4.setCellValue("怎么都行");

        //创建输出流对象
        OutputStream outputStream = new FileOutputStream(new File("C:\\Users\\123\\Desktop\\2.xlsx"));

        workbook.write(outputStream);

        outputStream.flush();
        outputStream.close();
        workbook.close();


    }




}
